//
//  PeopleListenViewController.m
//  海角音乐
//
//  Created by dllo on 15/8/18.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "PeopleListenViewController.h"

@interface PeopleListenViewController ()

@end

@implementation PeopleListenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    CGFloat title_width = SCREEN_WIDTH*0.533333; // 200
    CGFloat title_height = SCREEN_WIDTH*0.08; // 30
    
    self.TitleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, title_width, title_height)];
    self.TitleView.textColor = [UIColor whiteColor];
    self.TitleView.textAlignment = NSTextAlignmentCenter;
    self.TitleView.text = @"大家都在听";
    self.navigationItem.titleView = self.TitleView;
    
    _dataArr = [[NSMutableArray alloc] init];
    url_arr = [[NSMutableArray alloc] init];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64-50) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.removeFromSuperViewOnHide = YES;
    HUD.labelText = @"loading...";
    _tableView.separatorColor = [UIColor clearColor];
    [self.view addSubview:HUD];
    HUD.mode = MBProgressHUDModeIndeterminate;
   
    BarPlayer = [DanLiPlayer shareHandleDanLiPlayer:CGRectZero];
    [self.view addSubview:BarPlayer];

    
    [self getDataForPeopleListen];
    
    
    
    
    
    // Do any additional setup after loading the view.
}

- (void)getDataForPeopleListen
{
    
    
    NSString *url_string = AllPeopleListen;
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/x-javascript",nil];
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];

        
        NSArray *arr = [responseObject objectForKey:@"data"];
        
        for (NSDictionary *dic in arr) {
            ReDetailModel *model = [[ReDetailModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_dataArr addObject:model];
            [model release];
        }
        
        
        HUD.hidden = YES;
        _tableView.separatorColor = [UIColor lightGrayColor];
        [_tableView reloadData];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
    
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
    
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellid = @"cell";
    ReDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == NO) {
        
        cell = [[ReDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
    }
    
    if (_dataArr.count > indexPath.row) {
        
        ReDetailModel *model = [_dataArr objectAtIndex:indexPath.row];
    
        [cell getDataForCell:model];
    }
    
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row+1];
    
    cell.detailTextLabel.textColor = [UIColor whiteColor];
    return cell;
    
}

#pragma mark --  播放的逻辑
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    for (ReDetailModel *reDetailModel in _dataArr) {
        
        if (reDetailModel.auditionList.count > 1) {
            NSDictionary *dic = [reDetailModel.auditionList objectAtIndex:1];
            NSString *url_string = [dic objectForKey:@"url"];
            [url_arr addObject:url_string];
        } else {
            NSDictionary *dic = [reDetailModel.auditionList objectAtIndex:0];
            NSString *url_string = [dic objectForKey:@"url"];
            [url_arr addObject:url_string];
        }
        
    }
    
    
    ReDetailModel *reDetailmodel = [_dataArr objectAtIndex:indexPath.row];
    BarPlayer.reDetailModel = reDetailmodel;
    BarPlayer.dataArr = _dataArr;
    BarPlayer.index = indexPath.row;
    [self playeMusicWithModel:url_arr indexPath:indexPath];
    
    
    
}
// 播放方法
- (void)playeMusicWithModel:(NSMutableArray *)url_Arr indexPath:(NSIndexPath *)indexpath;
{
    
    
    NSString *url_string = [url_arr objectAtIndex:indexpath.row];
    
    STKAudioPlayer *Legacy = [SinglePlayer shareHandleSinglePlayer];
    
    [Legacy play:url_string];
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_WIDTH*0.2133333;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
